package online.westonline.com.westonline.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import online.westonline.com.westonline.R;
import online.westonline.com.westonline.entry.WorkItem;
import online.westonline.com.westonline.utils.UiUtlis;

/**
 * 工作模块适配器
 * Created by xiatao on 2018/8/1.
 */

public class WorkAdapter extends RecyclerView.Adapter<WorkAdapter.WorkViewHolder> {

    private List<WorkItem> datas = new ArrayList<>();
    private onClickListener clickListener;

    public void setClickListener(onClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public void setDatas(List<WorkItem> datas) {
        this.datas = datas;
    }

    @NonNull
    @Override
    public WorkViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.work_item, parent, false);
        return new WorkViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull WorkViewHolder holder, int position) {
        WorkItem workItem = datas.get(position);
        holder.tvWorkItem.setText(workItem.getTitle());
        holder.tvWorkItemDesc.setText(workItem.getDescrible());
        holder.imgWorkIcon.setBackgroundResource(workItem.getIcon_id());
    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    class WorkViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_work_icon)
        ImageView imgWorkIcon;
        @BindView(R.id.tv_work_item)
        TextView tvWorkItem;
        @BindView(R.id.tv_work_item_desc)
        TextView tvWorkItemDesc;
        public WorkViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != clickListener) {
                        clickListener.onClick(getLayoutPosition(), datas.get(getLayoutPosition()));
                    }
                }
            });
        }
    }

    public interface onClickListener {
        void onClick(int pos, WorkItem workItem);
    }
}
